package youcheng.work.view_zhexian;

//低功耗蓝牙适配器 三要素 上下文 蓝牙设备列表 信号强度指示

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class BleAdapter extends BaseAdapter {

private Context mContext;
private List<BluetoothDevice> mBluetoothDevices;//蓝牙设备列表
private List<Integer> mRssis;//存放整数的列表

public BleAdapter(Context mContext, List<BluetoothDevice> bluetoothDevices,List<Integer> rssis) {
        this.mContext = mContext;
        this.mBluetoothDevices = bluetoothDevices;
        mRssis=rssis;
        }

@Override
public int getCount() {
        return mBluetoothDevices.size();
        }

@Override
public Object getItem(int position) {
        return mBluetoothDevices.get(position);
        }

@Override
public long getItemId(int position) {
        return 0;
        }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            //获取view
        convertView = LayoutInflater.from(mContext).inflate(R.layout.device_item, null);
            //实例化类文件，获取数据格式
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
        } else {
        viewHolder = (ViewHolder) convertView.getTag();
        }
        //列表的每个项目就是一个设备
        BluetoothDevice device = (BluetoothDevice) getItem(position);
        //每个设备的名字 地址 和数字赋值
        viewHolder.name.setText(device.getName());
        viewHolder.introduce.setText(device.getAddress());
        viewHolder.tvRssi.setText(mRssis.get(position)+"");
        return convertView;
        }


class ViewHolder {
    public TextView name;
    public TextView introduce;
    public TextView tvRssi;
    public ViewHolder(View view) {
        name = view.findViewById(R.id.name);
        introduce =  view.findViewById(R.id.introduce);
        tvRssi=view.findViewById(R.id.rssi);
    }
}
}

